package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayOrderLogVo;
import org.dromara.system.domain.bo.PayOrderLogBo;
import org.dromara.system.service.IPayOrderLogService;

/**
 * 订单日志
 * 前端访问路由地址为:/system/orderLog
 *
 * @author Gavino
 * @date 2024-10-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/orderLog")
public class PayOrderLogController extends BaseController {

    private final IPayOrderLogService payOrderLogService;

    /**
     * 查询订单日志列表
     */
    @SaCheckPermission("system:orderLog:list")
    @GetMapping("/list")
    public TableDataInfo<PayOrderLogVo> list(PayOrderLogBo bo, PageQuery pageQuery) {
        return payOrderLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单日志列表
     */
    @SaCheckPermission("system:orderLog:export")
    @Log(title = "订单日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayOrderLogBo bo, HttpServletResponse response) {
        List<PayOrderLogVo> list = payOrderLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单日志", PayOrderLogVo.class, response);
    }

    /**
     * 获取订单日志详细信息
     *
     * @param logId 主键
     */
    @SaCheckPermission("system:orderLog:query")
    @GetMapping("/{logId}")
    public R<PayOrderLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long logId) {
        return R.ok(payOrderLogService.queryById(logId));
    }

    /**
     * 新增订单日志
     */
    @SaCheckPermission("system:orderLog:add")
    @Log(title = "订单日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayOrderLogBo bo) {
        return toAjax(payOrderLogService.insertByBo(bo));
    }

    /**
     * 修改订单日志
     */
    @SaCheckPermission("system:orderLog:edit")
    @Log(title = "订单日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayOrderLogBo bo) {
        return toAjax(payOrderLogService.updateByBo(bo));
    }

    /**
     * 删除订单日志
     *
     * @param logIds 主键串
     */
    @SaCheckPermission("system:orderLog:remove")
    @Log(title = "订单日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] logIds) {
        return toAjax(payOrderLogService.deleteWithValidByIds(List.of(logIds), true));
    }
}
